import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {SFSchema} from '@delon/form';
import {OuOrg} from '../../entity/ou-org';
import {OuOrgService} from '../../service/ou-org.service';
import {BaseService} from '@sb/base';
import {ActivatedRoute} from '@angular/router';

@Component({
    selector: 'ou-org-detail-tab',
    templateUrl: './ou-org-detail-tab.component.html',
    styleUrls: ['./ou-org-detail-tab.component.less'],
    providers: [{
        provide: BaseService,
        useClass: OuOrgService
    }]
})
export class OuOrgDetailTabComponent implements OnInit {

    @Output() dataChange = new EventEmitter<any>();

    schema: SFSchema = {
        properties: {
            orgName: {
                type: 'string',
                title: '机构名称',
            },
            // managerUserId: {
            //     type: 'string',
            //     title: '机构管理员',
            //     ui: {
            //         widget: 'ou-user-select-alain'
            //     }
            // },
            orgAddr: {
                type: 'string',
                title: '机构地址'
            },
            linkMan: {
                type: 'string',
                title: '联 系 人'
            },
            linkTel: {
                type: 'string',
                title: '联系电话'
            },
            email: {
                type: 'string',
                title: '电子邮箱'
            },
            webUrl: {
                type: 'string',
                title: '网站地址'
            }
        },
        required: ['orgName']
    };

    record: OuOrg;
    aclEdit = false;

    constructor(
        private orgService: OuOrgService,
        private activeRoute: ActivatedRoute) {
        this.activeRoute.data.subscribe(
            data => {
                const permissions = data.permissions;
                if (permissions) {
                    this.aclEdit = permissions.indexOf('edit') !== -1;
                }
            }
        );
    }

    _orgId: string;

    @Input()
    set orgId(_orgId: string) {
        this._orgId = _orgId;
        if (_orgId) {
            this.orgService.get(_orgId).subscribe(res => {
                if (res.success) {
                    this.record = res.message;
                }
            });
        }
    }

    ngOnInit() {
    }

    eventClose(value) {
        if (value) {
            this.dataChange.emit();
        }
    }

}
